<?php

namespace app\api\controller\v1;

use \app\common\controller\ApiCommon;
use \app\common\model\Room;
use \app\common\model\CourseRoom;


class Rooms extends ApiCommon
{
    use \app\common\model\TimeCommon;
    protected $modelName = 'Room';

    /**
     * RESTful 索引接口
     *
     * @return void
     */
    public function index()
    {
        $this->checkPermission(10);
        return parent::_index();
    }

    /**
     * RESTful 保存单个数据
     *
     * @return void
     */
    public function save()
    {
        $this->checkPermission(1);
        return parent::_save();
    }

    /**
     * RESTFul更新单个数据
     *
     * @return void
     */
    public function update($id)
    {
        $this->checkPermission(1);
        return parent::_update($id);
    }

    /**
     * 获取指定时间有课的课室
     * 请求参数:[building], date,[term_id]
     * @return void
     */
    public function getBusyRoom()
    {
        $request = $this->request;
        //获取日期
        if (!($date = $request->param('date'))) {
            return json_response([], STATUS_INVALD_PARAM, "必须传入日期数据");
        }

        $date2 = model('CourseAdjust')->getDate($date);

        if ($date2 === false) {
            return json_response([], STATUS_INVALD_PARAM, "此日期因换课被隐藏");
        }
        $date = str_replace("-0", "-", $date);
        $date2 = str_replace("-0", "-", $date2);
        if ($date != $date2) {
            $errmsg = "发生换课";
        } else {
            $errmsg = null;
        }

        $param = $this->getTimeDetail($date2, $request->param('term_id'));
        if (!$param['status']) {
            return json_response([], STATUS_FAIL, $param['errmsg']);
        }

        $course = CourseRoom::where(['term_id' => $param['term']->id, 'week' => $param['week']]);
        if ($building = $request->param('building')) {
            $rooms = Room::where('building', $building)->column('name');
            $course = $course->where('room', 'in', $rooms);
        }

        $course->whereRaw('(class_week&:class_week)<>0', ['class_week' => $param['class_week']]);
        $courses = $course->select();
        $table = [];
        foreach ($courses as $v) {
            if (isset($table[$v->room])) {
                $table[$v->room] |= $v->class_time;
            } else {
                $table[$v->room] = $v->class_time;
            }
        }

        ksort($table);
        $ret = [];
        $lastPerfix = null;
        foreach ($table as $k => $v) {
            if ($lastPerfix !== null && $lastPerfix != substr($k, 0, 3)) {
                $ret[] = ['name' => '', 'busy' => ["", "", ""]];
            }
            $ret[] = ['name' => $k, 'busy' => convertTimePeriod($v)];
            $lastPerfix = substr($k, 0, 3);
        }

        return json_response($ret, STATUS_OK, $errmsg);
    }


    /**
     * 获取所有房间的数据
     *
     * @return 所有教学楼信息,用于进行选择
     */
    public function getRoomNames()
    {
        return json_response(db('room')->column('name'));
    }

    /**
     * 获取所有教学楼的数据
     *
     * @return 所有教学楼信息,用于进行选择
     */
    public function getBuildingNames()
    {
        return json_response(db('room')->whereExp('building', 'is not null')->distinct(true)->column('building'));
    }
}
